/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Locale;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author KAV
 */
public abstract class AbstractBean implements EntityBean {

    public static final String DELETE="DELETE FROM ";
    public static final String SELECTBYID="SELECT id FROM ";
    public static final String WHEREID=" WHERE id=?";
    public static final String SELECTENTITY="SELECT * FROM ";
    public static final String SELECTMAXID="SELECT MAX(id) FROM ";
    protected DataSource ds;
    protected EntityContext context;

    protected abstract PreparedStatement queryForRemoveObj(Connection conn) throws SQLException;

    protected abstract PreparedStatement findBy(Connection conn, String byId) throws SQLException;

    protected abstract void loadEntity(PreparedStatement st, Connection conn) throws SQLException, NoSuchEntityException;

    protected abstract PreparedStatement storeEntity(Connection conn) throws SQLException;

    public void setEntityContext(EntityContext aContext) {
        try {
            context = aContext;
            Locale.setDefault(Locale.ENGLISH);
            Context c = new InitialContext();
            Object ods = c.lookup("jdbc/myconnection");
            ds = (DataSource) ods;
        } catch (NamingException ex) {
            throw new EJBException(ex);
        }
    }

    public void ejbActivate() {
    }

    public void ejbPassivate() {
    }

    public void ejbRemove() throws RemoveException {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = ds.getConnection();
            st = queryForRemoveObj(conn);
            st.setInt(1, Integer.parseInt(context.getPrimaryKey().toString()));
            if (st.executeUpdate() < 1) {
                throw new RemoveException("Bad removing.");
            }
        } catch (SQLException ex) {
            throw new EJBException(ex);
        } finally {
            Util.closeConnection(conn, st);
        }
    }

    public void unsetEntityContext() {
        context = null;
    }

    public void ejbLoad() {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = ds.getConnection();
            loadEntity(st, conn);
        } catch (SQLException ex) {
            throw new EJBException(ex);
        } finally {
            Util.closeConnection(conn, st);
        }
    }

    public void ejbStore() {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = ds.getConnection();
            st=storeEntity(conn);
            int rs = st.executeUpdate();
            if (rs < 1) {
                throw new NoSuchEntityException("Object can't insert(update)");
            }
        } catch (SQLException ex) {
            throw new EJBException(ex);
        } finally {
            Util.closeConnection(conn, st);
        }
    }

    public java.lang.Integer ejbFindByPrimaryKey(java.lang.Integer aKey) throws FinderException {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = ds.getConnection();
            st = findBy(conn, WHEREID);
            st.setInt(1, aKey.intValue());
            ResultSet resultSet = st.executeQuery();
            if (!resultSet.next()) {
                throw new ObjectNotFoundException("Object not found. Can create new object.");
            }
            return aKey;
        } catch (SQLException ex) {
            throw new EJBException(ex);
        } finally {
            Util.closeConnection(conn, st);
        }
    }

    public Collection ejbFindAll() {
        Collection c = new LinkedList();
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = ds.getConnection();
            st = findBy(conn,"");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                int keys = rs.getInt(1);
                c.add(new Integer(keys));
            }
        } catch (SQLException ex) {
            throw new EJBException(ex);
        } finally {
            Util.closeConnection(conn, st);
        }
        return c;
    }
}
